#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <strings.h>

int main(int argc, char** argv) {
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("socket");
		exit(errno);
	}
	
	struct sockaddr_in addr;
	bzero(&addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(2345);
	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);	
		
	if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
		perror("connect");
		exit(errno);
	}
	
	int addr_len = sizeof(addr);
	char send_buf[1024];
	char recv_buf[1024];
	printf("Start entering buffers to send:\n");
	while (1) {
		scanf("%s\n", send_buf);
		sendto  (sock, send_buf, sizeof(send_buf), 0, (struct sockaddr *)&addr, sizeof(addr));
		recvfrom(sock, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&addr, (socklen_t*)sizeof(addr));
		printf("%s\n", recv_buf);
	}
	close(sock);
	return 0;
}
